﻿// Pex.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "Pex.h"
#include "CManageEx.h"
#include "CToolbarEx.h"
#include "ShapeAttribute.h"
using namespace std;

#define MAX_LOADSTRING 100

// Global Variables:
HINSTANCE hInst;								// current instance
TCHAR szTitle[MAX_LOADSTRING];					// The title bar text
TCHAR szWindowClass[MAX_LOADSTRING];			// the main window class name
CManageEx manager;
CToolbarEx toolbar;
ShapeAttribute sAttribute;
bool allowSelect;
bool allowDraw;
bool allowDrawTextShape;
int curPenWidth = IDC_RADIO_SMALL;
int curPenStyle = IDC_RADIO_SOLID;



// Handle to BUTTON
HWND hBtnSelect;
HWND hBtnColor_1;
HWND hBtnRect;
HWND hBtnCir;
HWND hBtnDelete;
HWND hStatusBar;
HWND hDialog;
HWND hToolBar;

//HWND 
// Forward declarations of functions included in this code module:

// function prototypes
ATOM				MyRegisterClass(HINSTANCE hInstance);
BOOL				InitInstance(HINSTANCE, int);
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK	About(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK	DlgEditLine(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);
bool OpenFile(HWND);
bool SaveFile(HWND);
void SetShapeMode(HWND, UINT, int);
void AttributeInitialization(ShapeAttribute& SAttribute);
void ShowHideToolbar(bool);
void EnableButton(UINT);

int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,
						_In_opt_ HINSTANCE hPrevInstance,
						_In_ LPTSTR lpCmdLine,
						_In_ int nCmdShow)
{
	UNREFERENCED_PARAMETER(hPrevInstance);
	UNREFERENCED_PARAMETER(lpCmdLine);

 	// TODO: Place code here.
	MSG msg;
	HACCEL hAccelTable;

	// Initialize global strings
	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
	LoadString(hInstance, IDC_PEX, szWindowClass, MAX_LOADSTRING);
	MyRegisterClass(hInstance);

	// Perform application initialization:
	if (!InitInstance (hInstance, nCmdShow))
	{
		return FALSE;
	}

	hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_PEX));

	// Main message loop:
	while (GetMessage(&msg, NULL, 0, 0))
	{
		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}
	return (int) msg.wParam;
}



//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
	WNDCLASSEX wcex;

	wcex.cbSize			= sizeof(WNDCLASSEX);
	wcex.style			= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc	= WndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= hInstance;
	wcex.hIcon			= LoadIcon(hInstance, MAKEINTRESOURCE(IDI_PEX));
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName	= MAKEINTRESOURCE(IDC_PEX);
	wcex.lpszClassName	= szWindowClass;
	wcex.hIconSm		= LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

	return RegisterClassEx(&wcex);
}

//
//   FUNCTION: InitInstance(HINSTANCE, int)
//
//   PURPOSE: Saves instance handle and creates main window
//
//   COMMENTS:
//
//        In this function, we save the instance handle in a global variable and
//        create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
	HWND hWnd;

	hInst = hInstance; // Store instance handle in our global variable
	hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
						CW_USEDEFAULT,CW_USEDEFAULT,1366, 768,
						NULL, NULL, hInstance, NULL);
	if (!hWnd)
	{
		return FALSE;
	}

	ShowWindow(hWnd, SW_MAXIMIZE);
	UpdateWindow(hWnd);

	return TRUE;
}

//
//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_COMMAND	- process the application menu
//  WM_PAINT	- Paint the main window
//  WM_DESTROY	- post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	wchar_t buffer[20];
	PAINTSTRUCT ps;
	HDC hdc;
	
	hdc = GetDC(hWnd);
	switch(message)
	{
		case WM_CREATE:
		{
			hToolBar = toolbar.CreateMainFrame(hWnd, hInst, IDB_BITMAP);
			AttributeInitialization(sAttribute);
		
			SendMessage(hToolBar, TB_CHECKBUTTON,(WPARAM) IDC_BUTTON_BLACK,(LPARAM) MAKELONG(true, 0));	
			SendMessage(hToolBar, TB_CHECKBUTTON,(WPARAM) IDC_BUTTON_RECTANGLE,(LPARAM) MAKELONG(true, 0));	
			SendMessage(hToolBar, TB_CHECKBUTTON,(WPARAM) IDC_BUTTON_NOBG,(LPARAM) MAKELONG(true, 0));	

			allowDraw = true;
			allowSelect = false;
			allowDrawTextShape = false;

			hStatusBar = CreateWindow(TEXT("button"),
				TEXT("HELLO"),
				WS_CHILD | WS_VISIBLE,
				0, 660, 
				1366, 30,
				hWnd, 
				(HMENU)IDC_BUTTON_STATUSBAR,
				hInst, 
				NULL
				);
			break;
		}
	
		case WM_LBUTTONUP:
			if (allowSelect)
				manager.SelectShape(hdc, hWnd, message, wParam, lParam);
			else if (allowDraw)
				manager.DrawShape(hdc, hWnd, message, wParam, lParam, sAttribute);
			else if (allowDrawTextShape)
				manager.DrawTextShape(hdc, hWnd, message, wParam, lParam, sAttribute);
			break;

		case WM_LBUTTONDOWN:		
			if (allowSelect)
				manager.SelectShape(hdc, hWnd, message, wParam, lParam);
			else if (allowDraw )
				manager.DrawShape(hdc, hWnd, message, wParam, lParam, sAttribute);
			else if (allowDrawTextShape)
				manager.DrawTextShape(hdc, hWnd, message, wParam, lParam, sAttribute);
			break;

		case WM_MOUSEMOVE:
			if (allowSelect)
				manager.SelectShape(hdc, hWnd, message, wParam, lParam);
			else if (allowDraw)
				manager.DrawShape(hdc, hWnd, message, wParam, lParam, sAttribute);
			else if (allowDrawTextShape)
				manager.DrawTextShape(hdc, hWnd, message, wParam, lParam, sAttribute);

			/* Hien toa do chuot*/		
			wsprintf(buffer, L" %d, %d ", LOWORD(lParam), HIWORD(lParam));
			SetWindowText(hStatusBar, buffer);
			break;
		case WM_CHAR:
		case WM_KEYDOWN:
			manager.DrawTextShape(hdc, hWnd, message, wParam, lParam, sAttribute);
			break;
		case WM_COMMAND:
		{
			int wmId;
			//int wmEvent;
			wmId	= LOWORD(wParam);
			//wmEvent = HIWORD(wParam);
			
			// Parse the menu selections:

			switch (wmId)
			{
				// case button handle
				case IDC_BUTTON_SELECT:
					SetShapeMode(hWnd, ID_DRAW_SELECTOBJECT, _MENUDRAW_);
					break;
			
				case IDC_BUTTON_DELETE:
					manager.DeleteShape();
					InvalidateRect(hWnd, NULL, TRUE);		
					break;

				case IDC_BUTTON_SELECTLINE:
					DialogBox(hInst, MAKEINTRESOURCE(IDD_EDITLINE), hWnd, DlgEditLine);
					break;

				// case button SHAPE
				case IDC_BUTTON_RECTANGLE:
					sAttribute.pattern = _RECTANGLE_;
					InvalidateRect(hWnd, NULL, TRUE);
					SetShapeMode(hWnd, ID_DRAW_RECTANGLE, _MENUDRAW_);
					break;

				case IDC_BUTTON_CIRCLE:
					sAttribute.pattern = _CIRCLE_;
					InvalidateRect(hWnd, NULL, TRUE);
					SetShapeMode(hWnd, ID_DRAW_CIRCLE, _MENUDRAW_);
					break;

				case IDC_BUTTON_TRIANGLE:
					sAttribute.pattern = _TRIANGLE_;
					InvalidateRect(hWnd, NULL, TRUE);
					SetShapeMode(hWnd, ID_DRAW_TRIANGLE, _MENUDRAW_);
					break;

				case IDC_BUTTON_ROUNDRECTANGLE:
					sAttribute.pattern = _ROUNDRECTANGLE_;
					InvalidateRect(hWnd, NULL, TRUE);
					SetShapeMode(hWnd, ID_DRAW_ROUNDRECTANGLE, _MENUDRAW_);
					break;

				case IDC_BUTTON_LINE:
					sAttribute.pattern = _LINE_;
					InvalidateRect(hWnd, NULL, TRUE);
					SetShapeMode(hWnd, ID_DRAW_LINE, _MENUDRAW_);
					break;

				case IDC_BUTTON_TEXT:
					sAttribute.pattern = _TEXT_;

					InvalidateRect(hWnd, NULL, TRUE);
					SetShapeMode(hWnd, ID_DRAW_TEXT, _MENUDRAW_);
					
				// case button COLOR
				case IDC_BUTTON_RED:
					sAttribute.penColor = RGB(255, 0, 0);
					EnableButton(wmId);
					break;

				case IDC_BUTTON_BLACK:
					sAttribute.penColor = RGB(0, 0, 0);
					EnableButton(wmId);
					break;

				case IDC_BUTTON_BLUE:
					sAttribute.penColor = RGB(0, 0, 255);
					EnableButton(wmId);
					break;
				case IDC_BUTTON_BROWN:
					sAttribute.penColor = RGB(128, 0, 0);
					EnableButton(wmId);
					break;

				case IDC_BUTTON_GREEN:
					sAttribute.penColor = RGB(0, 255, 0);
					EnableButton(wmId);
					break;

				case IDC_BUTTON_YELLOW:
					sAttribute.penColor = RGB(255, 255, 0);
					EnableButton(IDC_BUTTON_YELLOW);
					break;

				case IDC_BUTTON_NAVY:
					sAttribute.penColor = RGB(0, 255, 255);
					EnableButton(wmId);
					break;

				case IDC_BUTTON_VIOLET:
					sAttribute.penColor = RGB(128, 0, 128);
					EnableButton(wmId);
					break;

				// case button brush color
				case IDC_BUTTON_FRED:
					sAttribute.filled = TRUE;
					sAttribute.brushColor = RGB(255, 0, 0);
					EnableButton(wmId);
					break;

				case IDC_BUTTON_FYELLOW:
					sAttribute.filled = TRUE;
					sAttribute.brushColor = RGB(255, 255, 0);
					EnableButton(wmId);
					break;

				case IDC_BUTTON_FBLUE:
					sAttribute.filled = TRUE;
					sAttribute.brushColor = RGB(0, 0, 255);
					EnableButton(wmId);
					break;

				case IDC_BUTTON_FBLACK:
					sAttribute.filled = TRUE;
					sAttribute.brushColor = RGB(0, 0, 0);
					EnableButton(wmId);
					break;

				case IDC_BUTTON_FGREEN:
					sAttribute.filled = TRUE;
					sAttribute.brushColor = RGB(0, 255, 0);
					EnableButton(wmId);
					break;

				case IDC_BUTTON_FBROWN:
					sAttribute.filled = TRUE;
					sAttribute.brushColor = RGB(128, 0, 0);
					EnableButton(wmId);
					break;

				case IDC_BUTTON_FNAVY:
					sAttribute.filled = TRUE;
					sAttribute.brushColor = RGB(0, 255, 255);
					EnableButton(wmId);
					break;

				case IDC_BUTTON_FVIOLET:
					sAttribute.filled = TRUE;
					sAttribute.brushColor = RGB(128, 0, 128);
					EnableButton(wmId);
					break;

				case IDC_BUTTON_NOBG:
					sAttribute.filled = false;
					EnableButton(wmId);
					break;

				// case menu file
				case ID_FILE_CREATE:
					int msgboxID;

					msgboxID = MessageBox(hWnd, L"Do you want to save this graphic ?", L"Warning",
						MB_ICONWARNING | 3);
					if (IDYES == msgboxID)
					{
						if (!SaveFile(hWnd))
						{
							MessageBox(hWnd, L"Failed to save this graphic! ", L"Error", 
							MB_ICONERROR | MB_OK);
							break;
						}
						else 
						{
							InvalidateRect(hWnd, NULL, TRUE);
							manager.CreateNewGraphic();
						}
					}
					else if (IDNO == msgboxID)
					{
						InvalidateRect(hWnd, NULL, TRUE);
						manager.CreateNewGraphic();	
						break;
					}
					else // IDCANCEL
					{
						//do nothing
					}		
					break;

				case ID_FILE_OPEN:
					if (!OpenFile(hWnd))
						MessageBox(hWnd, L"Failed to open *.dtl file! ", L"Error", 
							MB_ICONERROR | MB_OK);
					else 
						InvalidateRect(hWnd, NULL, TRUE);
					break;

				case ID_FILE_SAVE:
					if (!SaveFile(hWnd))
						MessageBox(hWnd, L"Failed to save this graphic !", L"Error",
						MB_ICONERROR | MB_OK);		
					break;

				case IDM_EXIT:
					DestroyWindow(hWnd);
					break;

				// case menu view
				case ID_VIEW_TOOLBAR:
					SetShapeMode(hWnd, ID_VIEW_TOOLBAR, _MENUVIEW_);	
					break;

				case ID_VIEW_STATUSBAR:
					SetShapeMode(hWnd, ID_VIEW_STATUSBAR, _MENUVIEW_);
					break;

				// casse menu help
				case IDM_ABOUT:
					DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
					break;
		
				// case menu draw
				case ID_DRAW_RECTANGLE:
					sAttribute.pattern = _RECTANGLE_;		
					SetShapeMode(hWnd, ID_DRAW_RECTANGLE, _MENUDRAW_);
					break;

				case ID_DRAW_CIRCLE:
					sAttribute.pattern = _CIRCLE_;
					SetShapeMode(hWnd, ID_DRAW_CIRCLE, _MENUDRAW_);
					break;

				case ID_DRAW_TRIANGLE:
					sAttribute.pattern = _TRIANGLE_;
					SetShapeMode(hWnd, ID_DRAW_TRIANGLE, _MENUDRAW_);		
					break;

				case ID_DRAW_LINE:
					sAttribute.pattern = _LINE_;
					SetShapeMode(hWnd, ID_DRAW_LINE, _MENUDRAW_);
					break;

				case ID_DRAW_SELECTOBJECT:
					SetShapeMode(hWnd, ID_DRAW_SELECTOBJECT, _MENUDRAW_);
					break;

				case ID_DRAW_TEXT:					
					SetShapeMode(hWnd, ID_DRAW_TEXT, _MENUDRAW_);
					break;
				default:
					return DefWindowProc(hWnd, message, wParam, lParam);
			}
			break;
		}
	
		case WM_PAINT:
			hdc = BeginPaint(hWnd, &ps);
			
			// TODO: Add any drawing code here...
			manager.ShowAll(hdc);
			EndPaint(hWnd, &ps);
			break;

		case WM_DESTROY:
			PostQuitMessage(0);
			break;

		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}

// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	UNREFERENCED_PARAMETER(lParam);
	switch (message)
	{
		case WM_INITDIALOG:
			return (INT_PTR)TRUE;

		case WM_COMMAND:
			if (IDOK == LOWORD(wParam) || IDCANCEL == LOWORD(wParam))
			{
				EndDialog(hDlg, LOWORD(wParam));
				return (INT_PTR)TRUE;
			}

		default:
					break;	//do nothing
	}
	return (INT_PTR)FALSE;
}

// dialog edit line 
INT_PTR CALLBACK DlgEditLine(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	UNREFERENCED_PARAMETER(lParam);
	switch (message)
	{
		case WM_INITDIALOG:
			SendMessage(GetDlgItem(hDlg, curPenWidth), BM_SETCHECK, 1, 0);
			SendMessage(GetDlgItem(hDlg, curPenStyle), BM_SETCHECK, 1, 0);	
			return (INT_PTR)TRUE;

		case WM_COMMAND:
			if (IDOK == LOWORD(wParam))
			{
				// pen width
				if (SendMessage(GetDlgItem(hDlg, IDC_RADIO_SMALL), BM_GETCHECK, wParam, lParam))
				{
					sAttribute.penWidth = 1;
					curPenWidth = IDC_RADIO_SMALL;
				}
				else if (SendMessage(GetDlgItem(hDlg, IDC_RADIO_MEDIUM), BM_GETCHECK, wParam, lParam))		 
				{
					sAttribute.penWidth = 2;
					curPenWidth = IDC_RADIO_MEDIUM;
				}
				else if (SendMessage(GetDlgItem(hDlg, IDC_RADIO_LARGE), BM_GETCHECK, wParam, lParam))		 
				{
					sAttribute.penWidth = 3;
					curPenWidth = IDC_RADIO_MEDIUM;
				}
				else 
				{
					sAttribute.penWidth = 4;
					curPenWidth = IDC_RADIO_HUGE;
				}
		
				//pen style	
				if (SendMessage(GetDlgItem(hDlg, IDC_RADIO_SOLID), BM_GETCHECK, wParam, lParam))
				{
					sAttribute.penStyle = PS_SOLID;
					curPenStyle = IDC_RADIO_SOLID;					
				}
				else if (SendMessage(GetDlgItem(hDlg, IDC_RADIO_DASHDOT), BM_GETCHECK, wParam, lParam)) 
				{
					sAttribute.penStyle = PS_DASHDOT;
					curPenStyle = IDC_RADIO_DASHDOT;
				}
				else 
				{
					sAttribute.penStyle = PS_DOT;
					curPenStyle = IDC_RADIO_DOT;
				}								
			
				EndDialog(hDlg, LOWORD(wParam));
				return (INT_PTR)TRUE;
			}
			else if (IDCANCEL == LOWORD(wParam))
			{
				EndDialog(hDlg, LOWORD(wParam));
				return (INT_PTR)TRUE;
			}
			break;

		default:
				break;	//do nothing
	}
	return (INT_PTR)FALSE;
}

//----------------------------------------------
// ULTILITY FUNCTIONS 
//----------------------------------------------

/* Set check and uncheck for menuitem of menu draw */
void SetShapeMode(HWND hWnd, UINT itemID, int menu)
{
	switch (menu)
	{
		case _MENUVIEW_:
			switch(itemID)
			{
				case ID_VIEW_TOOLBAR:
					if(MF_CHECKED == GetMenuState(GetMenu(hWnd), ID_VIEW_TOOLBAR, MF_BYCOMMAND))
					{
						CheckMenuItem(GetMenu(hWnd), ID_VIEW_TOOLBAR, MF_UNCHECKED);
						ShowHideToolbar(true);
					}
					else
					{
						CheckMenuItem(GetMenu(hWnd), ID_VIEW_TOOLBAR, MF_CHECKED);
						ShowHideToolbar(false);
					}
					break;
			}
			break;

		case _MENUDRAW_:
			CheckMenuItem(GetMenu(hWnd), ID_DRAW_RECTANGLE, MF_UNCHECKED);
			CheckMenuItem(GetMenu(hWnd), ID_DRAW_CIRCLE, MF_UNCHECKED);
			CheckMenuItem(GetMenu(hWnd), ID_DRAW_ROUNDRECTANGLE, MF_UNCHECKED);
			CheckMenuItem(GetMenu(hWnd), ID_DRAW_LINE, MF_UNCHECKED);
			CheckMenuItem(GetMenu(hWnd), ID_DRAW_TRIANGLE, MF_UNCHECKED);
			CheckMenuItem(GetMenu(hWnd), ID_DRAW_SELECTOBJECT, MF_UNCHECKED);
			CheckMenuItem(GetMenu(hWnd), ID_DRAW_TEXT, MF_UNCHECKED);
			CheckMenuItem(GetMenu(hWnd), itemID, MF_CHECKED);

			switch(itemID)
			{
				case ID_DRAW_RECTANGLE:
					EnableButton(IDC_BUTTON_RECTANGLE);
					break;

				case ID_DRAW_SELECTOBJECT:
					EnableButton(IDC_BUTTON_SELECT);
					break;

				case ID_DRAW_ROUNDRECTANGLE:
					EnableButton(IDC_BUTTON_ROUNDRECTANGLE);
					break;

				case ID_DRAW_CIRCLE:
					EnableButton(IDC_BUTTON_CIRCLE);
					break;

				case ID_DRAW_LINE:
					EnableButton(IDC_BUTTON_LINE);
					break;

				case ID_DRAW_TRIANGLE:
					EnableButton(IDC_BUTTON_TRIANGLE);
					break;
				case ID_DRAW_TEXT:
					EnableButton(IDC_BUTTON_TEXT);
					break;
				default:
					break;	//do nothing
			}
			break;

		default:
				break;	//do nothing
	}	
}

/* Open file */
bool OpenFile(HWND hWnd)
{
	
	OPENFILENAME ofn;		// common dialog box structure
	wchar_t szFile[260] = L"";	// buffer for file name
	// Initialize OPENFILENAME
	ZeroMemory(&ofn, sizeof(ofn));
	ofn.lStructSize = sizeof(ofn);
	ofn.hwndOwner = hWnd;
	ofn.lpstrFile = szFile;

	// Set lpstrFile[0] to '\0' so that GetOpenFileName does not
	// use the contents of szFile to initialize itself.
	ofn.lpstrFile[0] = '\0';
	ofn.nMaxFile = sizeof(szFile);
	ofn.lpstrFilter = L"*.dtl\0";
	ofn.nFilterIndex = 1;
	ofn.lpstrFileTitle = NULL;
	ofn.nMaxFileTitle = 0;
	ofn.lpstrInitialDir = NULL;
	ofn.Flags = OFN_EXPLORER | OFN_HIDEREADONLY | OFN_FILEMUSTEXIST;

	 //Display the Open dialog box.
	if (GetOpenFileName(&ofn))
	{		
		manager.OpenGraphic(hWnd, ofn.lpstrFile);
		InvalidateRect(hWnd, NULL, TRUE);
		return TRUE;
	}
	else 
		return FALSE;
}

/* Save file */
bool SaveFile(HWND hWnd)
{
	OPENFILENAME ofn;	
	wchar_t szFileName[MAX_PATH] = L"";
	ZeroMemory(&ofn, sizeof(ofn));
	ofn.lStructSize = sizeof(ofn); 
	ofn.hwndOwner = hWnd;
	ofn.nFileOffset = OFN_NOVALIDATE ;
	ofn.lpstrFilter = L"*.dtl\0";
	ofn.lpstrFile = szFileName;
	ofn.nMaxFile = MAX_PATH;
	ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
	ofn.lpstrDefExt = L"dtl";

	if (!GetSaveFileName(&ofn))
		return FALSE;
	else 
	{
		manager.SaveGraphic(hWnd, ofn.lpstrFile) ;
		return TRUE;
	}
}

/* Initialize shape attribute */
void AttributeInitialization(ShapeAttribute& SAttribute)
{
	SAttribute.penColor		= RGB(0, 0, 0);
	SAttribute.brushColor	= RGB(255, 255, 255);
	SAttribute.penStyle		= PS_SOLID;
	SAttribute.filled		= false;
	SAttribute.pattern		= _RECTANGLE_;
	SAttribute.penWidth		= 1;
}


/* Hide toolbar: hide shape and color */
void ShowHideToolbar(bool hide)
{
	SendMessage(hToolBar, TB_HIDEBUTTON,(WPARAM) IDC_BUTTON_RECTANGLE,(LPARAM) MAKELONG(hide, 0));	
	SendMessage(hToolBar, TB_HIDEBUTTON,(WPARAM) IDC_BUTTON_CIRCLE,(LPARAM) MAKELONG(hide, 0));	
	SendMessage(hToolBar, TB_HIDEBUTTON,(WPARAM) IDC_BUTTON_LINE,(LPARAM) MAKELONG(hide, 0));	
	SendMessage(hToolBar, TB_HIDEBUTTON,(WPARAM) IDC_BUTTON_ROUNDRECTANGLE,(LPARAM) MAKELONG(hide, 0));	
	SendMessage(hToolBar, TB_HIDEBUTTON,(WPARAM) IDC_BUTTON_TRIANGLE,(LPARAM) MAKELONG(hide, 0));	
	SendMessage(hToolBar, TB_HIDEBUTTON,(WPARAM) IDC_BUTTON_SELECT,(LPARAM) MAKELONG(hide, 0));	
	SendMessage(hToolBar, TB_HIDEBUTTON,(WPARAM) IDC_BUTTON_SELECTLINE,(LPARAM) MAKELONG(hide, 0));	

	SendMessage(hToolBar, TB_HIDEBUTTON,(WPARAM) IDC_BUTTON_BLACK,(LPARAM) MAKELONG(hide, 0));	
	SendMessage(hToolBar, TB_HIDEBUTTON,(WPARAM) IDC_BUTTON_BLUE,(LPARAM) MAKELONG(hide, 0));	
	SendMessage(hToolBar, TB_HIDEBUTTON,(WPARAM) IDC_BUTTON_BROWN,(LPARAM) MAKELONG(hide, 0));	
	SendMessage(hToolBar, TB_HIDEBUTTON,(WPARAM) IDC_BUTTON_GREEN,(LPARAM) MAKELONG(hide, 0));	
	SendMessage(hToolBar, TB_HIDEBUTTON,(WPARAM) IDC_BUTTON_YELLOW,(LPARAM) MAKELONG(hide, 0));	
	SendMessage(hToolBar, TB_HIDEBUTTON,(WPARAM) IDC_BUTTON_RED,(LPARAM) MAKELONG(hide, 0));	
	SendMessage(hToolBar, TB_HIDEBUTTON,(WPARAM) IDC_BUTTON_VIOLET,(LPARAM) MAKELONG(hide, 0));	
	SendMessage(hToolBar, TB_HIDEBUTTON,(WPARAM) IDC_BUTTON_NAVY,(LPARAM) MAKELONG(hide, 0));	
	
	SendMessage(hToolBar, TB_HIDEBUTTON,(WPARAM) IDC_BUTTON_FBLACK,(LPARAM) MAKELONG(hide, 0));	
	SendMessage(hToolBar, TB_HIDEBUTTON,(WPARAM) IDC_BUTTON_FBLUE,(LPARAM) MAKELONG(hide, 0));	
	SendMessage(hToolBar, TB_HIDEBUTTON,(WPARAM) IDC_BUTTON_FBROWN,(LPARAM) MAKELONG(hide, 0));	
	SendMessage(hToolBar, TB_HIDEBUTTON,(WPARAM) IDC_BUTTON_FGREEN,(LPARAM) MAKELONG(hide, 0));	
	SendMessage(hToolBar, TB_HIDEBUTTON,(WPARAM) IDC_BUTTON_FYELLOW,(LPARAM) MAKELONG(hide, 0));	
	SendMessage(hToolBar, TB_HIDEBUTTON,(WPARAM) IDC_BUTTON_FRED,(LPARAM) MAKELONG(hide, 0));	
	SendMessage(hToolBar, TB_HIDEBUTTON,(WPARAM) IDC_BUTTON_FVIOLET,(LPARAM) MAKELONG(hide, 0));	
	SendMessage(hToolBar, TB_HIDEBUTTON,(WPARAM) IDC_BUTTON_FNAVY,(LPARAM) MAKELONG(hide, 0));	
	SendMessage(hToolBar, TB_HIDEBUTTON,(WPARAM) IDC_BUTTON_NOBG,(LPARAM) MAKELONG(hide, 0));	
	SendMessage(hToolBar, TB_HIDEBUTTON,(WPARAM) IDC_BUTTON_TEXT,(LPARAM) MAKELONG(hide, 0));	

	SendMessage(hToolBar, TB_HIDEBUTTON,(WPARAM) ID_SEP1,(LPARAM) MAKELONG(hide, 0));	
	SendMessage(hToolBar, TB_HIDEBUTTON,(WPARAM) ID_SEP2,(LPARAM) MAKELONG(hide, 0));	
	SendMessage(hToolBar, TB_HIDEBUTTON,(WPARAM) ID_SEP3,(LPARAM) MAKELONG(hide, 0));	
	SendMessage(hToolBar, TB_HIDEBUTTON,(WPARAM) ID_SEP4,(LPARAM) MAKELONG(hide, 0));	
	SendMessage(hToolBar, TB_HIDEBUTTON,(WPARAM) ID_SEP5,(LPARAM) MAKELONG(hide, 0));	
	
}

/* Enable button */
void EnableButton(UINT itemID)
{
	UINT i;

	if (itemID <= IDC_BUTTON_SELECTLINE)
	{
		for (i = IDC_BUTTON_CIRCLE; i <= IDC_BUTTON_SELECTLINE; i++)
		{
			SendMessage(hToolBar, TB_CHECKBUTTON, (WPARAM) i, (LPARAM) MAKELONG(false, 0));	
		}
		switch ( itemID )
		{
		case IDC_BUTTON_TEXT:
			allowDrawTextShape = TRUE;
			allowDraw	  = FALSE;
			allowSelect   = FALSE;
			break;
		case IDC_BUTTON_SELECT:
			allowDraw	  = FALSE;
			allowDrawTextShape = FALSE;
			allowSelect	  = TRUE;
			break;
		default:
			allowDraw	  = TRUE;
			allowSelect	  = FALSE;
			allowDrawTextShape = FALSE;
		}
	}
	else if (itemID <= IDC_BUTTON_VIOLET)
		for (i = IDC_BUTTON_RED; i <= IDC_BUTTON_VIOLET; i++)
		{
			SendMessage(hToolBar, TB_CHECKBUTTON, (WPARAM) i, (LPARAM) MAKELONG(false, 0));	
		}
	else if (itemID <= IDC_BUTTON_NOBG)
		for (i = IDC_BUTTON_FRED; i <= IDC_BUTTON_NOBG; i++)
		{
			SendMessage(hToolBar, TB_CHECKBUTTON, (WPARAM) i, (LPARAM) MAKELONG(false, 0));	
		}

	SendMessage(hToolBar, TB_CHECKBUTTON, (WPARAM) itemID, (LPARAM) MAKELONG(true, 0));	
}
